package com.ruoyi.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.dto.SealDto;
import com.ruoyi.web.service.SealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/sdk")
public class SdkController {
    @Autowired
    private SealService sealService;

    @RequestMapping(value="/getSealBean")
    @ResponseBody
    public AjaxResult getSealBean(@RequestBody JSONObject data) {
        String sysId;
        String userId;
        String unitId;
        String sealType;
        SealDto sealDto = null;
        if (!data.containsKey("sysId")){
            return AjaxResult.error("认证失败");
        }
        sysId =  data.getString("sysId");
        if (data.containsKey("sealId")) {
            String sealId  = data.getString("sealId");
            sealDto = sealService.findSealById(sealId);
            if (sealDto ==null){
                //如果为空可能传入的是业务人员ID  这里设计 sealId既可以是签章系统总的印章ID，也可以有业务系统直接传入人员ID
                sealDto = sealService.findSealBySysPersonId(sealId,sysId);
            }
        }else if(data.containsKey("userId")){
            userId =  data.getString("userId");
            sealDto = sealService.findSealBySysPersonId(userId,sysId);
        }else if (data.containsKey("sealType")&&data.containsKey("unitId")){
            unitId =  data.getString("unitId");
            sealType =  data.getString("sealType");
            sealDto = sealService.findSealDtoByUnitAndType(unitId,sealType);
        }
        if(sealDto == null){
            return AjaxResult.error("找不到印章");
        }
        JSONObject js = new JSONObject();
        js.put("sealId",sealDto.getSealId());
        js.put("sealName",sealDto.getSealName());
        js.put("img",sealDto.getSeal_img());
        js.put("imgW",sealDto.getSeal_w());
        js.put("imgH",sealDto.getSeal_h());
        js.put("sealStandard",sealDto.getSeal_standard());
        js.put("pfx",sealDto.getSeal_pfx());
        js.put("password",sealDto.getSeal_pwd());
        return AjaxResult.success("成功",js);
    }
    @RequestMapping(value="/log")
    @ResponseBody
    public AjaxResult log(@RequestBody JSONObject data) {
        return AjaxResult.success("成功");
    }
}
